package com.hua.yuan.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.hua.yuan.R;
import com.hua.yuan.base.Constents;
import com.hua.yuan.ui.ActivityFuFJp;
import com.hua.yuan.ui.VideoDisActivity;
import com.hua.yuan.ui.data.VideoDataRequest;
import com.hua.yuan.ui.module.MovieInfo69Bean;
import com.hua.yuan.ui.module.MovieInfoBean;
import com.hua.yuan.util.CheckLoginUtil;
import com.zhy.http.okhttp.callback.StringCallback;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cm.hua.yuan.common.utils.PreferenceUtils;
import okhttp3.Call;

/**
 * Created by huangyang on 2018/5/9.
 * 第二个item
 */

public class VideoItemViewTwo extends RelativeLayout {

    private Context context;

    private List<MovieInfo69Bean> list;

    private LinearLayout videoMoreTipLl;//点击更多

    private ImageView videoImage1;
    private TextView videoItemName1;
    private TextView videoDsp1;
    private TextView videoPlayCount1;
    private TextView videoPlayMoney1;

    private ImageView videoImage2;
    private TextView videoItemName2;
    private TextView videoDsp2;
    private TextView videoPlayCount2;
    private TextView videoPlayMoney2;

    private ImageView videoImage3;
    private TextView videoItemName3;
    private TextView videoDsp3;
    private TextView videoPlayCount3;
    private TextView videoPlayMoney3;

    private ImageView videoImage4;
    private TextView videoItemName4;
    private TextView videoDsp4;
    private TextView videoPlayCount4;
    private TextView videoPlayMoney4;

    private ImageView videoImage5;
    private TextView videoItemName5;
    private TextView videoDsp5;
    private TextView videoPlayCount5;
    private TextView videoPlayMoney5;

    private ImageView videoImage6;
    private TextView videoItemName6;
    private TextView videoDsp6;
    private TextView videoPlayCount6;
    private TextView videoPlayMoney6;

    private LinearLayout video_fresh_ll;
    private ImageView video_fresh_image;

    private boolean flag = true;


    private void init(Context context) {
        this.context = context;
        LayoutInflater.from(context).inflate(R.layout.video_two_tms_two_view, this);
        initView();
        initData();


    }


    public VideoItemViewTwo(Context context) {
        super(context);
        init(context);
    }

    public VideoItemViewTwo(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);

    }

    public VideoItemViewTwo(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);

    }

    @SuppressLint("NewApi")
    public VideoItemViewTwo(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    public void setData(List<MovieInfo69Bean> list) {
        this.list = list;
        if (list != null) {

            if (list.size() > 0) {
                MovieInfo69Bean bean1 = list.get(0);
                showUi(videoImage1,
                        videoItemName1, videoDsp1,
                        videoPlayCount1, videoPlayMoney1, bean1);

            }

            if (list.size() > 1) {
                MovieInfo69Bean bean2 = list.get(1);
                showUi(videoImage2,
                        videoItemName2, videoDsp2,
                        videoPlayCount2, videoPlayMoney2, bean2);
            }

            if (list.size() > 2) {
                MovieInfo69Bean bean3 = list.get(2);
                showUi(videoImage3,
                        videoItemName3, videoDsp3,
                        videoPlayCount3, videoPlayMoney3, bean3);

            }

            if (list.size() > 3) {
                MovieInfo69Bean bean4 = list.get(3);
                showUi(videoImage4,
                        videoItemName4, videoDsp4,
                        videoPlayCount4, videoPlayMoney4, bean4);
            }

            if (list.size() > 4) {
                MovieInfo69Bean bean5 = list.get(4);
                showUi(videoImage5,
                        videoItemName5, videoDsp5,
                        videoPlayCount5, videoPlayMoney5, bean5);
            }

            if (list.size() > 5) {

                MovieInfo69Bean bean6 = list.get(5);
                showUi(videoImage6,
                        videoItemName6, videoDsp6,
                        videoPlayCount6, videoPlayMoney6, bean6);

            }


        }


    }


    private void initView() {

        videoMoreTipLl =  findViewById(R.id.videoMoreTipLl);

        videoImage1 =  findViewById(R.id.videoImage1);
        videoItemName1 =  findViewById(R.id.videoItemName1);
        videoDsp1 =  findViewById(R.id.videoDsp1);
        videoPlayCount1 =  findViewById(R.id.videoPlayCount1);
        videoPlayMoney1 =  findViewById(R.id.videoPlayMoney1);


        videoImage2 = findViewById(R.id.videoImage2);
        videoItemName2 =  findViewById(R.id.videoItemName2);
        videoDsp2 =  findViewById(R.id.videoDsp2);
        videoPlayCount2 =  findViewById(R.id.videoPlayCount2);
        videoPlayMoney2 =  findViewById(R.id.videoPlayMoney2);


        videoImage3 =  findViewById(R.id.videoImage3);
        videoItemName3 =  findViewById(R.id.videoItemName3);
        videoDsp3 =  findViewById(R.id.videoDsp3);
        videoPlayCount3 =  findViewById(R.id.videoPlayCount3);
        videoPlayMoney3 =  findViewById(R.id.videoPlayMoney3);

        videoImage4 =  findViewById(R.id.videoImage4);
        videoItemName4 =  findViewById(R.id.videoItemName4);
        videoDsp4 =  findViewById(R.id.videoDsp4);
        videoPlayCount4 =  findViewById(R.id.videoPlayCount4);
        videoPlayMoney4 =  findViewById(R.id.videoPlayMoney4);

        videoImage5 =  findViewById(R.id.videoImage5);
        videoItemName5 =  findViewById(R.id.videoItemName5);
        videoDsp5 =  findViewById(R.id.videoDsp5);
        videoPlayCount5 =  findViewById(R.id.videoPlayCount5);
        videoPlayMoney5 =  findViewById(R.id.videoPlayMoney5);

        videoImage6 =  findViewById(R.id.videoImage6);
        videoItemName6 =  findViewById(R.id.videoItemName6);
        videoDsp6 =  findViewById(R.id.videoDsp6);
        videoPlayCount6 =  findViewById(R.id.videoPlayCount6);
        videoPlayMoney6 =  findViewById(R.id.videoPlayMoney6);

        video_fresh_ll =  findViewById(R.id.video_fresh_ll);
        video_fresh_image =  findViewById(R.id.video_fresh_image);


    }

    private void initData() {

        videoMoreTipLl.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                context.startActivity(new Intent(context, ActivityFuFJp.class));

            }
        });

        video_fresh_ll.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                video_fresh_image.startAnimation(AnimationUtils.loadAnimation(context, R.anim.rotate_refresh));

                freshData();

            }
        });

        videoImage1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {

                if (list != null && list.size() > 0) {

                    if (CheckLoginUtil.check()) {
                        String id = list.get(0).getId();
                        context.startActivity(new Intent(context, VideoDisActivity.class).putExtra("id", id));
                    } else {
                        CheckLoginUtil.goToLogin((Activity) context);
                    }


                }
            }
        });

        videoImage2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {

                if (list != null && list.size() > 1) {

                    if (CheckLoginUtil.check()) {
                        String id = list.get(1).getId();
                        context.startActivity(new Intent(context, VideoDisActivity.class).putExtra("id", id));
                    } else {
                        CheckLoginUtil.goToLogin((Activity) context);
                    }


                }
            }
        });

        videoImage3.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {

                if (list != null && list.size() > 2) {

                    if (CheckLoginUtil.check()) {
                        String id = list.get(2).getId();
                        context.startActivity(new Intent(context, VideoDisActivity.class).putExtra("id", id));
                    } else {
                        CheckLoginUtil.goToLogin((Activity) context);
                    }


                }

            }
        });

        videoImage4.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {

                if (list != null && list.size() > 3) {

                    if (CheckLoginUtil.check()) {
                        String id = list.get(3).getId();
                        context.startActivity(new Intent(context, VideoDisActivity.class).putExtra("id", id));
                    } else {
                        CheckLoginUtil.goToLogin((Activity) context);
                    }


                }

            }
        });

        videoImage5.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {

                if (list != null && list.size() > 4) {

                    if (CheckLoginUtil.check()) {
                        String id = list.get(4).getId();
                        context.startActivity(new Intent(context, VideoDisActivity.class).putExtra("id", id));
                    } else {
                        CheckLoginUtil.goToLogin((Activity) context);
                    }


                }

            }
        });

        videoImage6.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {

                if (list != null && list.size() > 5) {

                    if (CheckLoginUtil.check()) {
                        String id = list.get(5).getId();
                        context.startActivity(new Intent(context, VideoDisActivity.class).putExtra("id", id));
                    } else {
                        CheckLoginUtil.goToLogin((Activity) context);
                    }


                }

            }
        });


    }


    @SuppressLint("SetTextI18n")
    private void showUi(ImageView imageView,
                        TextView tv1, TextView tv2,
                        TextView tv3, TextView tv4, MovieInfoBean bean) {

        if (bean == null) {
            return;
        }

        //显示图片
        if (context == null) {
            return;
        }

        if (imageView == null) {
            return;
        }

        if (tv1 == null) {
            return;
        }
        if (tv2 == null) {
            return;
        }
        if (tv3 == null) {
            return;
        }
        if (tv4 == null) {
            return;
        }


        //显示图片
        Glide.with(context)
                .load(bean.getImg_url())
                .into(imageView);
        //显示名字
        tv1.setText(bean.getTitle());

        //显示描述
        tv2.setText(bean.getDesc());

        //显示播放数
        int num;
        try {
            num = Integer.parseInt(bean.getPlay_nums());
        } catch (NumberFormatException e) {
            num = 0;
        }
        if (num > 999) {
            tv3.setText("播放数：999+");
        } else {
            tv3.setText("播放数：" + num);
        }

        //显示代币
        if ("1".equals(bean.getIs_free())) {
            tv4.setVisibility(GONE);
        } else {
            //显示代币
            tv4.setVisibility(VISIBLE);
            tv4.setText(bean.getScores() + "代币");
        }


    }


    /**
     * 换一批数据
     */
    private void freshData() {

        if (flag) {

            flag = false;
            String token = PreferenceUtils.getString(Constents.TOKEN, "");

            final int from = PreferenceUtils.getInt(Constents.FU_FEI_DATA_START, 0);

            VideoDataRequest.getMovies(token, 2, from, 6, new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {

                    flag = true;


                }

                @Override
                public void onResponse(String response, int id) {

                    flag = true;

                    try {
                        JSONObject jsonObject = new JSONObject(response);

                        if (!jsonObject.isNull("errno")) {
                            String code = jsonObject.getString("errno");

                            if ("0".equals(code)) {

                                if (!jsonObject.isNull("data")) {

                                    JSONArray dataJson = jsonObject.getJSONArray("data");

                                    Gson gson = new Gson();

                                    List<MovieInfo69Bean> list = new ArrayList<>();

                                    for (int i = 0; i < dataJson.length(); i++) {
                                        JSONObject data = dataJson.getJSONObject(i);
                                        MovieInfo69Bean bean = gson.fromJson(data.toString(), MovieInfo69Bean.class);
                                        list.add(bean);
                                    }

                                    if (list.size() >= 6) {
                                        PreferenceUtils.setInt(Constents.FU_FEI_DATA_START, from + 6);
                                        setData(list);
                                    }

                                    if (list.size() == 0) {
                                        PreferenceUtils.setInt(Constents.FU_FEI_DATA_START, 0);

                                    }


                                }


                            }

                        }


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


                }
            });


        }


    }


}
